package com.example.springbootjpasqllogdemo.service.impl;

import com.example.springbootjpasqllogdemo.common.result.Result;
import com.example.springbootjpasqllogdemo.common.result.ResultEnum;
import com.example.springbootjpasqllogdemo.common.util.AssertUtils;
import com.example.springbootjpasqllogdemo.common.util.PubUtils;
import com.example.springbootjpasqllogdemo.domain.QUserRecord;
import com.example.springbootjpasqllogdemo.domain.UserRecord;
import com.example.springbootjpasqllogdemo.dto.UserRecordListDTO;
import com.example.springbootjpasqllogdemo.dto.UserRecordSaveOrUpdateDTO;
import com.example.springbootjpasqllogdemo.repository.UserRecordRepository;
import com.example.springbootjpasqllogdemo.service.UserRecordService;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 系统用户档案表 业务逻辑层 具体实现
 *
 * @author Lv
 * @date 2022/11/4
 */
@Service
public class UserRecordServiceImpl implements UserRecordService {
    @Autowired
    private UserRecordRepository userRecordRepository;

    @Autowired
    private ModelMapper modelMapper;

    /**
     * 保存用户档案数据
     *
     * @param userRecordSaveOrUpdateDTO 用户档案添加对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> save(UserRecordSaveOrUpdateDTO userRecordSaveOrUpdateDTO) {
        UserRecord userRecord = modelMapper.map(userRecordSaveOrUpdateDTO, UserRecord.class);
        userRecord.setCreatetime(new Date());
        UserRecord tUserRecord = userRecordRepository.save(userRecord);
        return Result.success(tUserRecord.getId());
    }

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    /**
     * 分页查询用户档案列表
     *
     * @param userRecordListDTO 用户档案查询对象
     * @return
     */
    @Override
    public Result<?> load(UserRecordListDTO userRecordListDTO) {
        QUserRecord qUserRecord = QUserRecord.userRecord;

        // 条件构造
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        if (PubUtils.isNotEmpty(userRecordListDTO.getName())) {
            booleanBuilder.and(qUserRecord.name.like("%" + userRecordListDTO.getName() + "%"));
        }
        if (PubUtils.isNotEmpty(userRecordListDTO.getProfession())) {
            booleanBuilder.and(qUserRecord.profession.eq(userRecordListDTO.getProfession()));
        }
        // 分页数据
        Integer pageIndex = userRecordListDTO.getPageNo();
        Pageable pageable = PageRequest.of(pageIndex - 1, userRecordListDTO.getPageSize());

        // 数据提取
        QueryResults<UserRecord> userRecordQueryResults = jpaQueryFactory.selectFrom(qUserRecord)
                .where(booleanBuilder)
                .orderBy(qUserRecord.id.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();

        Page<UserRecord> pageData = new PageImpl<UserRecord>(userRecordQueryResults.getResults(), pageable, userRecordQueryResults.getTotal());
        return Result.success(pageData);
    }

    /**
     * 获取详情
     *
     * @param id 用户档案id
     * @return
     */
    @Override
    public Result<?> getById(Integer id) {
        UserRecord userRecord = userRecordRepository.getById(id);
        AssertUtils.notNull(userRecord, ResultEnum.PARAM_INVALID);
        return Result.success(userRecord);
    }

    /**
     * 修改用户档案数据
     *
     * @param id                        用户档案id
     * @param userRecordSaveOrUpdateDTO 用户档案修改对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> update(Integer id, UserRecordSaveOrUpdateDTO userRecordSaveOrUpdateDTO) {
        UserRecord userRecord = modelMapper.map(userRecordSaveOrUpdateDTO, UserRecord.class);
        userRecord.setId(id);
        userRecordRepository.save(userRecord);
        return Result.success();
    }

    /**
     * 删除用户档案
     *
     * @param id 用户档案id
     * @return
     */
    @Override
    public Result<?> delete(Integer id) {
        userRecordRepository.deleteById(id);
        return Result.success();
    }
}
